<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../../assets/logo.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="outUser">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏导航 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle" @click="toggleMenu">|||</div>
        <el-menu
          background-color="#373d41"
          text-color="#fff"
          :default-active="this.$route.path"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          @select="addTags"
          unique-opened
          router
        >
          <el-submenu :index="item.id+''" v-for="(item,index) of menuList" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconList[index]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+items.path" v-for="items of item.children" :key="items.id">
              <!-- 二级菜单 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{items.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体区域 -->
      <el-main>
        <tags-view></tags-view>
        <transition name="fade" mode="out-in">
          <router-view class="main" />
        </transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import tagsView from "@/components/tagsView.vue";
export default {
  name: "home",
  data() {
    return {
      menuList: [],
      iconList: [
        //一级菜单图标
        "el-icon-s-custom",
        "el-icon-s-cooperation",
        "el-icon-s-goods",
        "el-icon-s-claim",
        "el-icon-s-marketing"
      ],
      isCollapse: false //默认张开菜单
    };
  },
  components: {
    tagsView
  },
  methods: {
    outUser() {
      //退出功能，清除token，并且跳转登录页
      window.sessionStorage.clear();
      this.$store.commit("crearTags"); //清除标签
      this.$router.push("/login");
    },
    async getMenuList() {
      //左侧导航数据
      const { data: res } = await this.$axios.get("menus");
      if (res.meta.status === 200) {
        this.menuList = res.data;
      }
    },
    toggleMenu() {
      //折叠菜单
      this.isCollapse = !this.isCollapse;
    },
    addTags(index) {
      window.console.log(index);
      if (index === this.$route.path) return;
      this.$store.commit("addTags", index);
    }
  },
  created() {
    this.getMenuList();
  }
};
</script>

<style lang='stylus' scoped>
.home-container {
  height: 100%;
}

.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #373d41;
  font-size: 22px;
  align-items: center;
  color: #fff;

  div {
    display: flex;
    align-items: center;

    img {
      width: 60px;
      height: 60px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .toggle {
    color: #fff;
    font-size: 12px;
    text-align: center;
    line-height: 25px;
    background-color: #4a5064;
    user-select: none;
    cursor: pointer;
    letter-spacing: 0.1em;
  }
}

.el-menu {
  border-right: 0;
}

.main {
  margin-top: 10px;
}

.el-main {
  background-color: #eaedf1;
  padding: 10px 20px;
}

.fade-enter-active, .fade-leave-active {
  transition: all 0.5s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}

.fade-enter-to {
  transform: translateX(0);
}

.fade-leave-to {
  transform: translateX(20px);
}
</style>